package com.meteor.backController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meteor.pojo.Type;
import com.meteor.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/type")
public class BackTypeController {

    @Resource
    private TypeService typeService;

    /**
     * 博客分类分页
     */
    @GetMapping("/{pageNum}")
    public String page(@PathVariable int pageNum,Model model) {
        PageHelper.startPage(pageNum,5);
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Type::getId);
        List<Type> list = typeService.list(queryWrapper);
        PageInfo<Type> pageInfo = new PageInfo<>(list);
        model.addAttribute("page",pageInfo);
        model.addAttribute("types",list);
        return "admin/types";
    }

    /**
     * 分类实现新增
     */
    @GetMapping("/add")
    @Transactional
    public String add(Type type) {
        typeService.save(type);
        return "redirect:/type/1";
    }

    /**
     * 分类修改(未成功)
     */
    @GetMapping("/update/{id}")
    public String update(@PathVariable Integer id,Type type) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type.getId());
        type.setId(id);
        typeService.update(type,wrapper);
        return "redirect:/type/1";
    }

    /**
     * 分类删除
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        typeService.removeById(id);
        return "redirect:/type/1";
    }

}















